package com.saifan.wyy_ov.ui.onlishop;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.CommentBean;
import com.saifan.wyy_ov.data.bean.MyOrderBean;
import com.saifan.wyy_ov.data.bean.StoreBean;
import com.saifan.wyy_ov.data.bean.SubmitOrderBean;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.utils.ImgUtil;
import com.saifan.wyy_ov.utils.PriceUtils;
import com.saifan.wyy_ov.utils.TimeUtil;
import com.saifan.wyy_ov.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单适配器
 *
 * @author zjt
 */
public class AdapterOrder extends BaseAdapter {
    Context context;
    List<MyOrderBean> myOrderBeans;
    Handler handler;
    protected static final int DELETEORDER = 1234;

    public List<MyOrderBean> getMyOrderBeans() {
        return myOrderBeans;
    }

    public void setMyOrderBeans(List<MyOrderBean> myOrderBeans) {
        this.myOrderBeans = myOrderBeans;
        notifyDataSetChanged();
    }

    public AdapterOrder(Context context, List<MyOrderBean> list, Handler handler) {
        this.context = context;
        this.myOrderBeans = list;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        if (myOrderBeans!=null && myOrderBeans.size() > 0)
            return myOrderBeans.size();
        else
            return 0;
    }

    @Override
    public Object getItem(int position) {
        if (myOrderBeans!=null&&!myOrderBeans.isEmpty())
            return myOrderBeans.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        //  MyOrderBean myOrderBean = myOrderBeans.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.my_order_view, null);
            viewHolder.ll_store = (LinearLayout) convertView.findViewById(R.id.ll_store);
            viewHolder.iv_store_img = (com.saifan.wyy_ov.ui.view.CircleImageView) convertView.findViewById(R.id.iv_store_img);
            viewHolder.tv_store_name = (TextView) convertView.findViewById(R.id.tv_store_name);
            viewHolder.tv_order_state = (TextView) convertView.findViewById(R.id.tv_order_state);
            viewHolder.lv_order_detail = (com.saifan.wyy_ov.ui.view.NoScrollListView) convertView.findViewById(R.id.lv_order_details);
            viewHolder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);
            viewHolder.tv_total = (TextView) convertView.findViewById(R.id.tv_total);
            viewHolder.bt_del_order = (Button) convertView.findViewById(R.id.bt_del_order);
            viewHolder.bt_discuss = (Button) convertView.findViewById(R.id.bt_discuss);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ImgUtil.DisPlayImg(myOrderBeans.get(position).getStoreHeadPhoto(), viewHolder.iv_store_img, context);
        viewHolder.tv_store_name.setText(myOrderBeans.get(position).getStoreName() + " ");
        viewHolder.tv_count.setText(myOrderBeans.get(position).getTotal() + "");
        viewHolder.tv_total.setText(PriceUtils.getPrice(myOrderBeans.get(position).getTotalPrice()) + "元");
        if (myOrderBeans.get(position).getOrderState() == 1) {
            viewHolder.tv_order_state.setText("待付款");
            viewHolder.bt_del_order.setVisibility(View.VISIBLE);
            viewHolder.bt_discuss.setVisibility(View.VISIBLE);
            viewHolder.bt_del_order.setText("取消订单");
            viewHolder.bt_discuss.setText("付款");

        } else if (myOrderBeans.get(position).getOrderState() == 2) {
            viewHolder.tv_order_state.setText("待配送");
            viewHolder.bt_discuss.setVisibility(View.GONE);
            viewHolder.bt_del_order.setVisibility(View.GONE);
        } else if (myOrderBeans.get(position).getOrderState() == 3) {
            viewHolder.tv_order_state.setText("配送中");
            viewHolder.bt_discuss.setVisibility(View.GONE);
            viewHolder.bt_del_order.setVisibility(View.GONE);
        } else if (myOrderBeans.get(position).getOrderState() == 4) {
            viewHolder.tv_order_state.setText("退款中");
            viewHolder.bt_discuss.setVisibility(View.GONE);
            viewHolder.bt_del_order.setVisibility(View.GONE);
        } else if (myOrderBeans.get(position).getOrderState() == 5) {
            viewHolder.tv_order_state.setText("已退款");
            viewHolder.bt_discuss.setVisibility(View.GONE);
            viewHolder.bt_del_order.setVisibility(View.GONE);
        } else if (myOrderBeans.get(position).getOrderState() == 6) {
            viewHolder.tv_order_state.setText("已完成");
            viewHolder.bt_discuss.setVisibility(View.VISIBLE);
            viewHolder.bt_discuss.setText("评论订单");
            viewHolder.bt_del_order.setVisibility(View.VISIBLE);
            viewHolder.bt_del_order.setText("删除订单");
        }  else if (myOrderBeans.get(position).getOrderState() == 7) {
            viewHolder.tv_order_state.setText("已取消");
            viewHolder.bt_discuss.setVisibility(View.GONE);
            viewHolder.bt_del_order.setVisibility(View.VISIBLE);
            viewHolder.bt_del_order.setText("删除订单");
        }

        viewHolder.tv_time.setText(myOrderBeans.get(position).getTime());
        if (!myOrderBeans.get(position).getOrderDetail().isEmpty() && myOrderBeans.get(position).getOrderDetail().size() > 0) {
            AdapterOrderdetail adapterOrderdetail = new AdapterOrderdetail(context, myOrderBeans.get(position).getOrderDetail(), myOrderBeans.get(position));
            viewHolder.lv_order_detail.setAdapter(adapterOrderdetail);
            adapterOrderdetail.notifyDataSetChanged();
        }
        viewHolder.bt_del_order.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                
                CommentBean commentBean = new CommentBean();
                commentBean.setID(myOrderBeans.get(position).getID() + "");
                GetData getData = new GetData();
                String tip = "正在删除,请稍候！";
                if (myOrderBeans.get(position).getOrderState() == 1) {
                    tip = "正在取消,请稍候!";
                    getData.getDataJson(context, Api.GroupBuy.CANCELORDER, commentBean, tip, new ResponseData() {

                        @Override
                        public void onSuccess(String json) {
                            ToastUtil.showToast(context, "操作成功");
                            Message msg = new Message();
                            msg.what = DELETEORDER;
                            msg.arg1 = myOrderBeans.get(position).getID();
                            handler.sendMessage(msg);

                        }

                        @Override
                        public void onFailed(String str) {
                            ToastUtil.showToast(context, "操作失败");
                        }
                    });
                } else if (myOrderBeans.get(position).getOrderState() == 6 || myOrderBeans.get(position).getOrderState() == 7) {
                    getData.getDataJson(context, Api.GroupBuy.DELETEORDER, commentBean, tip, new ResponseData() {


                        @Override
                        public void onSuccess(String json) {
                            ToastUtil.showToast(context, "操作成功");
                            Message msg = new Message();
                            msg.what = DELETEORDER;
                            msg.arg1 = myOrderBeans.get(position).getID();
                            handler.sendMessage(msg);

                        }

                        @Override
                        public void onFailed(String str) {
                            ToastUtil.showToast(context, "操作失败");
                        }
                    });
                }

            }
        });
        viewHolder.bt_discuss.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                
                // 未付款
                if (myOrderBeans.get(position).getOrderState() == 1) {
                    GetData getData = new GetData();
                    List<SubmitOrderBean> submitOrderBeans = new ArrayList<SubmitOrderBean>();
                    SubmitOrderBean submitOrderBean = new SubmitOrderBean();
                    int paymentWay = myOrderBeans.get(position).getPaymentWay();
                    double total = Double.valueOf(myOrderBeans.get(position).getTotalPrice());
                    submitOrderBean.setAmount(total + "");
                    submitOrderBean.setID(myOrderBeans.get(position).getID());
                    submitOrderBean.setOrderNumber(myOrderBeans.get(position).getOrderNumber());
                    submitOrderBeans.add(submitOrderBean);
                    Intent intentGoPayActivity = new Intent(context, PayActivity.class);
                    intentGoPayActivity.putExtra("paymentWay", paymentWay);

                    intentGoPayActivity.putExtra("total", total);
                    intentGoPayActivity.putExtra("SubmitOrderBeans", new Gson().toJson(submitOrderBeans));
                    context.startActivity(intentGoPayActivity);
                }
                // 已付款 评价
                else if (myOrderBeans.get(position).getOrderState() == 6) {
                    Intent intent = new Intent(context, SubmitCommentsActivity.class);
                    intent.putExtra("StoreID", myOrderBeans.get(position).getStoreID());
                    context.startActivity(intent);
                }
            }
        });
        viewHolder.ll_store.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                
                Intent intent = new Intent(context, StoreDetailsActivity.class);
                StoreBean storeBean = new StoreBean();
                storeBean.setID(myOrderBeans.get(position).getStoreID());
                storeBean.setStoreName(myOrderBeans.get(position).getStoreName());
                storeBean.setHeadPhoto(myOrderBeans.get(position).getStoreHeadPhoto());
                storeBean.setTel(myOrderBeans.get(position).getStoreTel());
                storeBean.setBusinessDate(myOrderBeans.get(position).getBusinessDate());
                storeBean.setBusinessTime(myOrderBeans.get(position).getBusinessTime());
                storeBean.setAddress(myOrderBeans.get(position).getStoreAddress());
                storeBean.setIsDelivery(true);
                intent.putExtra("data", storeBean);
                context.startActivity(intent);
            }
        });
        viewHolder.lv_order_detail.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position2, long id) {
                
                Intent intent = new Intent(context, MyOrderDetailsActivity.class);
                intent.putExtra("position", position);
                Bundle bundle = new Bundle();
                bundle.putSerializable("myOrderBean", myOrderBeans.get(position));
                intent.putExtras(bundle);

                ((Activity) context).startActivityForResult(intent, 1);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        // Content View Elements
        public com.saifan.wyy_ov.ui.view.CircleImageView iv_store_img;
        public TextView tv_store_name;
        public TextView tv_order_state;
        public com.saifan.wyy_ov.ui.view.NoScrollListView lv_order_detail;
        public TextView tv_count;
        public TextView tv_total;
        public TextView tv_time;
        public Button bt_del_order;
        public Button bt_discuss;
        public LinearLayout ll_store;
        // End Of Content View Elements
    }
}
